﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GoodsReportManage.ItemClass;

namespace GoodsReportManage.Goods
{
    public partial class GoodsFind : Form
    {
        public GoodsFind()
        {
            InitializeComponent();
        }
        
        public event EventHandler FillGoodsEvent; //使用默认的事件处理委托

        SqlBaseClass G_SqlClass = new SqlBaseClass();
        WinOperationClass G_OperationForm = new WinOperationClass();  //声明对窗体操作的类对象
        int G_Int_status;  //保存操作按钮数据

        private void BindData()
        {
            string P_Str_cmdtxt = "SELECT GoodsID as 商品ID,GoodsName as 商品名称,GoodsPrice as 价格 FROM tb_Goods";
            this.dgvGoodsInfo.DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
        }
        private void GoodsFind_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if ( string.IsNullOrEmpty(txtGoodsName.Text)|| !G_OperationForm.IsNumeric(this.txtPrice.Text) )
            {
                MessageBox.Show("您输入的数据格式有误！", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string P_Str_cmdtxt = "INSERT INTO tb_Goods(GoodsName,GoodsPrice)";
                P_Str_cmdtxt += " VALUES('" + this.txtGoodsName.Text + "'," + txtPrice.Text + ")";
                //执行SQL语句并返回执行结果
                if (G_SqlClass.GetExecute(P_Str_cmdtxt))
                {

                    MessageBox.Show("数据添加成功！");
                }
                else
                {
                    MessageBox.Show("数据添加失败！");
                }
            }
        }

        private void dgvGoodsInfo_DoubleClick(object sender, EventArgs e)
        {
            if (FillGoodsEvent != null)
            {
                GoodsEventArgs args = new GoodsEventArgs();
                args.GoodsName = this.dgvGoodsInfo[1, this.dgvGoodsInfo.CurrentCell.RowIndex].Value.ToString();
                float price = 0;
                float.TryParse(this.dgvGoodsInfo[2, this.dgvGoodsInfo.CurrentCell.RowIndex].Value.ToString(), out price);
                args.GoodsPrice = price;
                FillGoodsEvent(sender, args);
                this.Close();
            }
            
        }
        private void FillControls()
        {
            try
            {
                this.txtGoodsName.Text = this.dgvGoodsInfo[1, this.dgvGoodsInfo.CurrentCell.RowIndex].Value.ToString();
                this.txtPrice.Text = this.dgvGoodsInfo[2, this.dgvGoodsInfo.CurrentCell.RowIndex].Value.ToString();
            }
            catch { }
        }

        private void ControlStatus()
        {
            this.toolSave.Enabled = !this.toolSave.Enabled;
            this.toolAdd.Enabled = !this.toolAdd.Enabled;
            this.toolCancel.Enabled = !this.toolCancel.Enabled;
            this.toolAmend.Enabled = !this.toolAmend.Enabled;

            this.txtGoodsName.ReadOnly = !this.txtGoodsName.ReadOnly;
            this.txtPrice.ReadOnly = !this.txtPrice.ReadOnly;
           
        }

        private void ClearControls()
        {
            this.txtGoodsName.Text = "";
            this.txtPrice.Text = "";
            
        }

        private void toolAdd_Click(object sender, EventArgs e)
        {
            ControlStatus();
            ClearControls();
            G_Int_status = 1;
        }

        private void toolAmend_Click(object sender, EventArgs e)
        {
            ControlStatus();
            G_Int_status = 2;
        }

        private void toolDelete_Click(object sender, EventArgs e)
        {
            string P_Str_condition = this.dgvGoodsInfo[0, this.dgvGoodsInfo.CurrentCell.RowIndex].Value.ToString();
            if (string.IsNullOrEmpty(P_Str_condition))
            {
                MessageBox.Show("没有数据！");
                return;
            }
            string P_Str_cmdtxt = "DELETE FROM tb_Goods WHERE GoodsID=" + P_Str_condition + "";
            if (G_SqlClass.GetExecute(P_Str_cmdtxt))
            {
                MessageBox.Show("数据删除成功！");
            }
            else
            {
                MessageBox.Show("数据删除失败！");
            }
            BindData();
        }

        private void toolCancel_Click(object sender, EventArgs e)
        {
            ControlStatus();
            ClearControls();
        }

        private void toolSave_Click(object sender, EventArgs e)
        {
            string P_Str_condition, P_Str_cmdtxt;
            switch (G_Int_status)
            {
                case 1:
                    //下面是要执行的SQL语句
                    P_Str_cmdtxt = "INSERT INTO tb_Goods(GoodsName,GoodsPrice)";
                    P_Str_cmdtxt += " VALUES('" + this.txtGoodsName.Text + "','" + this.txtPrice.Text + "')";

                    if (this.txtGoodsName.Text == "")
                    {
                        MessageBox.Show("商品名称不能为空！", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        //执行SQL语句并返回执行结果
                        if (G_SqlClass.GetExecute(P_Str_cmdtxt))
                        {

                            MessageBox.Show("数据添加成功！");
                            ControlStatus();
                            ClearControls();
                        }
                        else
                        {
                            MessageBox.Show("数据添加失败！");

                        }
                    }
                    break;
                case 2:
                    P_Str_condition = this.dgvGoodsInfo[0, this.dgvGoodsInfo.CurrentCell.RowIndex].Value.ToString();
                    P_Str_cmdtxt = "UPDATE tb_Goods SET GoodsName='" + this.lbGoodsName.Text + "',GoodsPrice='" + this.txtPrice.Text + "'";
                  
                    if (G_SqlClass.GetExecute(P_Str_cmdtxt))
                    {
                        MessageBox.Show("数据修改成功！");
                        ControlStatus();
                    }
                    else
                    {
                        MessageBox.Show("数据修改失败！");
                    }
                    break;
                default:
                    break;
                //绑定数据显示控件
            }
            BindData();
        }
    }
}
